package com.example.mall.web.controller;


import com.example.mall.doc.ProductsDoc;
import com.example.mall.query.ProductsQueryDto;
import com.example.mall.result.JSONResult;
import com.example.mall.result.PageList;
import com.example.mall.service.IESProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: xxxliu
 * @CreateTime: 2025-05-20
 * @Version: 1.0
 */
@RestController
@RequestMapping("/search")
public class ESProductsController {

    @Autowired
    private IESProductService productService;
    //添加课程
    @RequestMapping(value = "/addProduct",method = RequestMethod.POST)
    public JSONResult addProduct(@RequestBody ProductsDoc doc){
        productService.save(doc);
        return JSONResult.success();
    }

    //批量添加课程
    @RequestMapping(value = "/batchAddProduct",method = RequestMethod.POST)
    public JSONResult batchAddProduct(@RequestBody ProductsDoc[] docs){
        productService.saveAll(docs);
        return JSONResult.success();
    }

    // 3. 根据ID获取商品
    @GetMapping("/{id}")
    public JSONResult getById(@PathVariable Long id) {
        ProductsDoc productsDoc = productService.getById(id);
        return JSONResult.success(productsDoc);
    }

    @DeleteMapping("/{id}")
    public JSONResult deleteById(@PathVariable Long id) {
        productService.deleteById(id);
        return JSONResult.success();
    }

    //删除课程
    @RequestMapping(value = "/deleteProduct",method = RequestMethod.POST)
    public JSONResult deleteProduct(@RequestBody ProductsDoc doc){
        productService.delete(doc);
        return JSONResult.success();
    }

    //搜索课程
    @RequestMapping(value = "/searchProduct",method = RequestMethod.POST)
    public JSONResult searchProduct(@RequestBody ProductsQueryDto dto){
        PageList<ProductsDoc> pageList = productService.searchProducts(dto);
        return JSONResult.success(pageList);
    }
}
